package com.example.monthdateselector;

import hirondelle.date4j.DateTime;

import java.util.List;
import java.util.TimeZone;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SelectorAdapter extends BaseAdapter {
	private Context context;
	private List<CalDayItem> data;

	public SelectorAdapter(Context context, List<CalDayItem> data) {
		this.context = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public CalDayItem getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_cal_cell, null);
		}
		TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
		tv1.setGravity(Gravity.CENTER);
		CalDayItem cdi=data.get(position);
		tv1.setText(cdi.getText());
		DateTime d=DateTime.today(TimeZone.getDefault());
		if(cdi.getDate()!=null){
			bg_real_normal=cdi.getDate().getMonth()%2==0?bg_normal1:bg_normal2;
		}
		convertView.setBackgroundColor(bg_real_normal);
		boolean isToday=cdi.getDate()==null?false:d.isSameDayAs(cdi.getDate());
		setCellDisPlay(cdi.getStatus(), tv1, isToday);
		tv1.setTag(cdi);
		return convertView;
	}
	

	public static final int STATUS_EMPTY=1;
	public static final int STATUS_NORMAL = 0;
	public static final int STATUS_DISABLE = 2;
	public static final int STATUS_SELECTED_HEAD = 3;
	public static final int STATUS_SELECTED_BODY = 4;
	public static final int STATUS_SELECTED_TAIL = 5;
	public static final int STATUS_EDITED_HEAD = 6;
	public static final int STATUS_EDITED_BODY = 7;
	public static final int STATUS_EDITED_TAIL = 8;
	
	public int bg_disable = Color.GRAY;
	public int bg_normal1= Color.WHITE; // 没有背景
	public int bg_normal2 = Color.LTGRAY; // 没有背景
	
	private int bg_real_normal=bg_normal1;
	
	public int bg_selected_head = R.drawable.report_cal_select_head;
	public int bg_selected_body = R.drawable.report_cal_select_body;
	public int bg_selected_tail = R.drawable.report_cal_select_tail;

	public int bg_edited_head = R.drawable.report_cal_edit_head;
	public int bg_edited_body =R.drawable.report_cal_edit_body;
	public int bg_edited_tail = R.drawable.report_cal_edit_tail;

	public int text_normal = Color.RED;
	public int text_selected_edited = Color.WHITE;

	public int text_disable = Color.DKGRAY;
	
	public int text_today=Color.BLUE;
	
	public String today_text="今天";
	
	/** 根据状态设置外观(背景和文字颜色) */
	private void setCellDisPlay(int state, TextView v,boolean isToady) {
		int bgRes = 0;
		int textColor = 0;
		v.setVisibility(View.VISIBLE);
		switch (state) {
		case STATUS_DISABLE:
			bgRes = 0;
//			v.setBackgroundColor(bg_disable);
			textColor = text_disable;
			break;
		case STATUS_SELECTED_HEAD:
			bgRes = bg_selected_head;
			textColor = text_selected_edited;
			break;
		case STATUS_SELECTED_BODY:
			bgRes = bg_selected_body;
			textColor = text_selected_edited;
			break;
		case STATUS_SELECTED_TAIL:
			bgRes = bg_selected_tail;
			textColor = text_selected_edited;
			break;
		case STATUS_EDITED_HEAD:
			bgRes = bg_edited_head;
			textColor = text_selected_edited;
			break;
		case STATUS_EDITED_BODY:
			bgRes = bg_edited_body;
			textColor = text_selected_edited;
			break;
		case STATUS_EDITED_TAIL:
			bgRes = bg_edited_tail;
			textColor = text_selected_edited;
			break;
		case STATUS_EMPTY:
			v.setVisibility(View.INVISIBLE);
			break;
		case STATUS_NORMAL:
		default:
			bgRes = 0;
//			v.setBackgroundColor(bg_real_normal);
			textColor = isToady?text_today:text_normal;
			break;
		}
		if (v != null) {
			if(isToady){
				v.setText(today_text);
			}
			if (bgRes != 0) {
				v.setBackgroundResource(bgRes);
			}
			if (textColor != 0) {
				v.setTextColor(textColor);
			}
		}

	}

}
